package com.ruoyi.web.controller.procured;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.commodity.service.IProductService;
import com.ruoyi.commodity.service.IVendorService;
import com.ruoyi.common.core.domain.Company;
import com.ruoyi.common.core.domain.ProcuredOrders;
import com.ruoyi.common.core.domain.Vendor;
import com.ruoyi.common.core.domain.depot.Monetary;
import com.ruoyi.common.core.domain.depot.PreWarehouse;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.depot.service.IMonetaryService;
import com.ruoyi.depot.service.IPreWarehouseService;
import com.ruoyi.procured.domain.ProcuredPlanProcess;
import com.ruoyi.procured.domain.ProcuredProduct;
import com.ruoyi.procured.domain.TbProcuredProduct;
import com.ruoyi.procured.domain.vo.ProcuredPlanLogVo;
import com.ruoyi.procured.domain.vo.ProcuredPlanVo;
import com.ruoyi.procured.service.IProcuredOrdersService;
import com.ruoyi.procured.service.IProcuredPlanLogService;
import com.ruoyi.procured.service.IProcuredPlanProcessService;
import com.ruoyi.sale.domain.PlanProcess;
import com.ruoyi.sale.domain.vo.PlanVo;
import com.ruoyi.system.service.ICompanyService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.procured.domain.ProcuredPlan;
import com.ruoyi.procured.service.IProcuredPlanService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购计划Controller
 * 
 * @author ruoyi
 * @date 2023-11-03
 */
@RestController
@RequestMapping("/procured/plan")
public class ProcuredPlanController extends BaseController
{
    @Autowired
    private IProcuredPlanService procuredPlanService;

    @Autowired
    private IVendorService vendorService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private IPreWarehouseService preWarehouseService;

    @Autowired
    private IMonetaryService monetaryService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IProcuredOrdersService procuredOrdersService;

    @Autowired
    private IProcuredPlanLogService procuredPlanLogService;

    @Autowired
    private IProcuredPlanProcessService procuredPlanProcessService;

    /**
     * 查询采购计划列表
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProcuredPlan procuredPlan)
    {
        startPage();
        List<ProcuredPlan> list = procuredPlanService.selectProcuredPlanList(procuredPlan);
        return getDataTable(list);
    }

    /**
     * 导出采购计划列表
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:export')")
    @Log(title = "采购计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProcuredPlan procuredPlan)
    {
        List<ProcuredPlan> list = procuredPlanService.selectProcuredPlanList(procuredPlan);
        ExcelUtil<ProcuredPlan> util = new ExcelUtil<ProcuredPlan>(ProcuredPlan.class);
        util.exportExcel(response, list, "采购计划数据");
    }

    /**
     * 获取采购计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:query')")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId)
    {
        return success(procuredPlanService.selectProcuredPlanByPlanId(planId));
    }

    /**
     * 获取采购计划详情中的订单信息(采购计划详情页面获取信息）
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:query')")
    @GetMapping(value = "/orders/details")
    public TableDataInfo getDetails(ProcuredOrders procuredOrders)
    {
        startPage();
        List<ProcuredOrders> ordersList = procuredPlanService.selectProcuredPlanByDetails(procuredOrders);
        return getDataTable(ordersList);
    }

    /**
     * 获取采购计划采购商品的详细
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:query')")
    @GetMapping(value = "/{planId}/products")
    public AjaxResult getPlanProducts(@PathVariable("planId") Long planId)
    {
        ProcuredPlan ordersList = procuredPlanService.getPlanProducts(planId);
        return AjaxResult.success(ordersList);
    }

    /**
     * 新增采购计划
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:add')")
    @Log(title = "采购计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProcuredPlan procuredPlan, @RequestParam("componentPath") String componentPath)
    {
        return toAjax(procuredPlanService.insertProcuredPlan(procuredPlan,getUserId(),componentPath));
    }

    /**
     * 修改采购计划
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:edit')")
    @Log(title = "采购计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProcuredPlan procuredPlan)
    {
        return toAjax(procuredPlanService.updateProcuredPlan(procuredPlan));
    }

    /**
     * 删除采购计划
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:remove')")
    @Log(title = "采购计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds)
    {
        return toAjax(procuredPlanService.deleteProcuredPlanByPlanIds(planIds));
    }

    /**
     * 供应商
     * @param vendor
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('procured:plan:add')")
    @GetMapping("/selectVendorTreeList")
    public AjaxResult selectVendorTreeList(Vendor vendor){
        return success(vendorService.selectVendorTreeList(vendor));
    }

    /**
     * 机构管理
     * @param company
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('procured:plan:add')")
    @GetMapping("/selectCompanyTreeList")
    public AjaxResult selectCompanyTreeList(Company company){
        return success(companyService.selectCompanyTreeList(company));
    }

    /**
     * 查询所在的部门名称
     * @param deptId
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('procured:plan:add')")
    @GetMapping("/selectDeptById/{deptId}")
    public AjaxResult selectDeptById(@PathVariable Long deptId)
    {
        deptService.checkDeptDataScope(deptId);
        return success(deptService.selectDeptById(deptId));
    }

    /**
     * 仓库列表
     * @param preWarehouse
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('procured:plan:add')")
    @GetMapping("/selectPreWarehouseTreeList")
    public AjaxResult selectPreWarehouseTreeList(PreWarehouse preWarehouse){
        return success(preWarehouseService.selectPreWarehouseTreeList(preWarehouse));
    }

    /**
     * 货币单位
     * @param monetary
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('procured:plan:add')")
    @GetMapping("/selectMonetaryTreeList")
    public AjaxResult selectMonetaryTreeList(Monetary monetary){
        return success(monetaryService.selectMonetaryTreeList(monetary));
    }

    /**
     * 批量查询产品
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('procured:plan:add')")
    @GetMapping("/selectProductListById/{ids}")
    public AjaxResult selectProductListById(@PathVariable Long[] ids){
        return success(productService.selectProductListById(ids));
    }

    @PreAuthorize("@ss.hasPermi('procured:plan:approved')")
    @GetMapping("/updateProcuredPlanStatus/{planIds}/{flay}")
    public AjaxResult updateProcuredPlanStatus(@PathVariable Long[] planIds, @PathVariable boolean flay){
        return toAjax(procuredPlanService.updateProcuredPlanStatus(planIds,flay,getNickname()));
    }


    /**
     * 查询采购产品列表
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:batch')")
    @GetMapping("/selectTbProcuredProductList")
    public TableDataInfo selectTbProcuredProductList(TbProcuredProduct tbProcuredProduct)
    {
        startPage();
        List<TbProcuredProduct> list = procuredPlanService.selectTbProcuredProductList(tbProcuredProduct);
        return getDataTable(list);
    }

    /**
     * 批量查询采购产品数据
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:batch')")
    @GetMapping("/selectTbProcuredProductListById/{ids}")
    public AjaxResult selectTbProcuredProductListById(@PathVariable Long[] ids){
        return success(procuredPlanService.selectTbProcuredProductListById(ids));
    }

    @PreAuthorize("@ss.hasPermi('procured:plan:batch')")
    @PutMapping("/updateProcuredProductSelectedStatus/{ids}/{flay}")
    public AjaxResult updateProcuredProductSelectedStatus(@PathVariable Long[] ids,@PathVariable boolean flay){
        return success(procuredPlanService.updateProcuredProductSelectedStatus(ids,flay));
    }

    /**
     * 分批下单插入相关订单数据
     * @param procuredPlan
     * @return
     */
    @PreAuthorize("@ss.hasPermi('procured:plan:batch')")
    @PostMapping("/insertProcuredOrders")
    public AjaxResult insertProcuredOrders(@RequestBody ProcuredPlan procuredPlan){
        return toAjax(procuredPlanService.insertProcuredOrders(procuredPlan,getNickname()));
    }

    @PreAuthorize("@ss.hasPermi('procured:plan:batch')")
    @GetMapping("/selectProcuredOrdersPlanId/{planId}")
    public AjaxResult selectProcuredOrdersPlanId(@PathVariable Long planId){
        return success(procuredOrdersService.selectProcuredOrdersPlanId(planId));
    }

    @PreAuthorize("@ss.hasPermi('procured:plan:batch')")
    @DeleteMapping("/deleteProcuredOrdersByOrdersNumber/{ordersNumbers}")
    public AjaxResult deleteProcuredOrdersByOrdersNumber(@PathVariable String[] ordersNumbers){
        return toAjax(procuredPlanService.deleteProcuredOrdersByOrdersNumber(ordersNumbers));
    }

    @PreAuthorize("@ss.hasPermi('procured:plan:batch')")
    @PutMapping("/updateProcuredPlanOrdersNumber/{ordersNumbers}/{flay}")
    public AjaxResult updateProcuredPlanOrdersNumber(@PathVariable String[] ordersNumbers,@PathVariable boolean flay){
        return success(procuredPlanService.updateProcuredPlanOrdersNumber(ordersNumbers,flay));
    }

    /**
     * 待我审核的采购计划
     * @param procuredPlan
     * @return 结果
     */
    @GetMapping("/getPlanWaitProcuredProcessList")
    public TableDataInfo getPlanWaitProcuredProcessList(ProcuredPlan procuredPlan)
    {
        if (procuredPlan == null) { // 这里的XXX需要替换为plan对象中的属性名
            startPage();
        }
        List<ProcuredPlan> list = procuredPlanService.getPlanWaitProcuredProcessList(procuredPlan);
        return getDataTable(list);
    }
    /**
     * 我已审核的采购计划
     * @param procuredPlan
     * @return
     */
    @GetMapping("/getPlanReadyProcuredProcessList")
    public TableDataInfo getPlanReadyProcuredProcessList(ProcuredPlanVo procuredPlan){
        startPage();
        List<ProcuredPlanVo> list = procuredPlanProcessService.getPlanReadyProcuredProcessList(procuredPlan);
        return getDataTable(list);
    }
    /**
     * 我发起的采购计划
     * @param procuredPlan
     * @return
     */
    @GetMapping("/getPlanMyProcuredProcessList")
    public TableDataInfo getPlanMyProcuredProcessList(ProcuredPlanVo procuredPlan){
        startPage();
        List<ProcuredPlanVo> list = procuredPlanProcessService.getPlanMyProcuredProcessList(procuredPlan);
        return getDataTable(list);
    }
    /**
     * 更新审批结果
     * @param procuredPlanProcess
     * @return 结果
     */
    @PutMapping("/updateProcuredPlanProcessStatus")
    public AjaxResult updateProcuredPlanProcessStatus(@RequestBody ProcuredPlanProcess procuredPlanProcess){
        procuredPlanProcess.setReviewerId(getUserId());
        return success(procuredPlanProcessService.updateProcuredPlanProcessStatus(procuredPlanProcess));
    }
    /**
     * 查询审核流程
     * @param procuredPlanId
     * @return
     */
    @GetMapping("/getProcuredPlanProcessList/{procuredPlanId}")
    public AjaxResult getPlanProcessList(@PathVariable Long procuredPlanId){
        return success(procuredPlanProcessService.getProcuredPlanProcessList(procuredPlanId));
    }
    /**
     * 查询审核记录数据
     * @param procuredPlanLog
     * @return
     */
    @GetMapping("/selectPlanProcessLogList")
    public TableDataInfo selectPlanProcessLogList(ProcuredPlanLogVo procuredPlanLog){
        startPage();
        List<ProcuredPlanLogVo> planProcessLogs = procuredPlanLogService.selectProcuredPlanLogList(procuredPlanLog);
        return getDataTable(planProcessLogs);
    }
}
